package com.hhw.openglcamera;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.hst.cz.cameralib.WlCameraView;

public class FloatViewCamera360 implements WlCameraView.ICallback{
	private WindowManager mWindowManager = null;
	private LayoutParams mWmParams = null;
	private Context mContext = null;
	private View mView = null;
	private static final String TAG = "Touch360";
	private WlCameraView mCameraView = null;

	public FloatViewCamera360() {
		if (mView == null){
			mContext = APP.getContext();
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			mView = inflater.inflate(R.layout.float_360, null);

			initWin(0, 80, 768,400);
			showView(false);
		}
	}

	public FloatViewCamera360(Activity ct) {
		if (mView == null){
			mContext = ct;
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			mView = inflater.inflate(R.layout.float_360, null);

			initWin(0, 80, 768,400);
		}
	}

	@SuppressLint("WrongConstant")
	private void initWin(int x, int y, int w, int h) {
		if (mView != null) {
			mWindowManager = (WindowManager) mContext.getSystemService("window");
			mWmParams = new LayoutParams();
			mWmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
					| LayoutParams.FLAG_NOT_FOCUSABLE;
			mWmParams.gravity = Gravity.BOTTOM; // 调整悬浮窗口至左上角
			mWmParams.x = x;
			mWmParams.y = y;
			mWmParams.width = w;
			mWmParams.height = h;
			mWindowManager.addView(mView, mWmParams);

			mCameraView = mView.findViewById(R.id.float_surface);
			mCameraView.setFps(10);
			mCameraView.setCallback(this);
		}
	}

	public void dismiss(){
		//变成1像素，不能实际移除，否则SurfaceView会调用onDestroy
		mCameraView.setRenderMode(WlCameraView.RENDERMODE_WHEN_DIRTY);//切换为手动刷新模式（暂停刷新）
		mWmParams.width = 1;
		mWmParams.height = 1;
		mWindowManager.updateViewLayout(mView, mWmParams);
	}

	public void show() {
		mWmParams.width = 768;
		mWmParams.height = 400;
		mWindowManager.updateViewLayout(mView, mWmParams);

		mCameraView.setRenderMode(WlCameraView.RENDERMODE_CONTINUOUSLY);//切换为自动刷新模式
        mCameraView.requestRender();//刷新
	}

	public View getView() {
		return mView;
	}

	public void showView(boolean b) {

		if (mView == null){
			if (b) Log.i(TAG,"show 360,but view is NULL");
			return;
		}

		if (b) {
			getView().setVisibility(View.VISIBLE);
			mWindowManager.updateViewLayout(mView, mWmParams);
		} else {
			getView().setVisibility(View.GONE);
		}
	}

	public boolean isShow(){
		if (mView == null){
			return false;
		}

		return mView.getVisibility() == View.VISIBLE&&mWmParams.height>1;
	}

	@Override
	public void onCameraViewCreated() {
		Log.i(TAG,"onCameraViewCreated2");
		new Handler(Looper.getMainLooper()).post(()->{
			mView.findViewById(R.id.dialog_360_process).setVisibility(View.GONE);
		});
	}

	@Override
	public void onCameraViewDestroyed() {
		Log.i(TAG,"onCameraViewDestroyed");
	}

	@Override
	public void onCameraViewError(Exception e) {

	}


}
